package com.qf.java2007.web.controller;

import com.qf.java2007.pojo.BookInfo;
import com.qf.java2007.pojo.ClassInfo;
import com.qf.java2007.service.BookInfoService;
import com.qf.java2007.service.ClassInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @Author AzureSky_X
 * @Date 2021/3/8 19:10
 * @Version 1.0
 */
@Controller
@RequestMapping("/book")
//如果不想每次都写private  final Logger logger = LoggerFactory.getLogger(XXX.class); 可以用注解@Slf4j
@Slf4j
public class BookController {
    @Autowired
    private BookInfoService bookInfoService;

    @Autowired
    private ClassInfoService classInfoService;

    /**
     * 列出所有书籍信息
     *
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list() {
        log.info("这是BookController中的list...");
        List<BookInfo> list = bookInfoService.findAll();
        ModelAndView mav = new ModelAndView("bookinfo-list");
        mav.addObject("bookInfos", list);
        return mav;
    }

    /**
     * 查看书籍详情
     *
     * @param bookId
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(Long bookId) {
        log.info("这是BookController中的detail...", "bookId:" + bookId);
        BookInfo bookInfo = bookInfoService.findByIdWithClass(bookId);
        ModelAndView mav = new ModelAndView("bookinfo-detail");
        mav.addObject("bookInfo", bookInfo);
        return mav;
    }

    /**
     * 根据用户输入的关键字模糊查询出书籍信息
     *
     * @param searchWord
     * @return
     */
    @PostMapping("/searchBook")
    public ModelAndView search(String searchWord) {

        log.info("BookController search....{}", searchWord);

        List<BookInfo> list = bookInfoService.findByNameOrAuthor(searchWord);
        ModelAndView mav = new ModelAndView("bookinfo-list");
        mav.addObject("bookInfos", list);
        mav.addObject("searchWord", searchWord);
        return mav;
    }

    /**
     * 删除用户所选书籍
     *
     * @param bookId
     * @return
     */
    @GetMapping("/deleteBook")
    public ModelAndView delete(Long bookId) {
        bookInfoService.delete(bookId);
        List<BookInfo> list = bookInfoService.findAll();
        ModelAndView mav = new ModelAndView("bookinfo-list");
        mav.addObject("bookInfos", list);

        mav.addObject("succ", "删除成功！！！");
        return mav;
    }

    /**
     * 跳转到增加图书UI界面
     *
     * @return
     */
    @GetMapping("/addUI")
    public ModelAndView addUI() {
        ModelAndView mav = new ModelAndView("bookinfo-add");
        List<ClassInfo> classInfoList = classInfoService.findAll();
        mav.addObject("classInfoList", classInfoList);
        return mav;
    }

    /**
     * 添加书籍
     *
     * @param bookInfo
     * @return
     */
    @PostMapping("/addBook")
    public ModelAndView add(BookInfo bookInfo) {
        log.info("添加的书籍信息：", bookInfo);
        bookInfoService.add(bookInfo);
        List<BookInfo> list = bookInfoService.findAll();
        ModelAndView mav = new ModelAndView("bookinfo-list");
        mav.addObject("bookInfos", list);
        mav.addObject("succ", "增加成功！！！");
        return mav;
    }

    /**
     * 回显要修改的图书信息
     *
     * @param bookId
     * @return
     */
    @GetMapping("/updateBook")
    public ModelAndView updateUI(Long bookId) {
        Map<String, Object> map = bookInfoService.findById(bookId);
        ModelAndView mav = new ModelAndView("bookinfo-update");
        mav.addObject("bookInfo", map.get("bookInfo"));
        mav.addObject("classInfoList", map.get("classInfo"));
        return mav;
    }

    /**
     * 传入修改信息，进行修改
     *
     * @param bookInfo
     * @return
     */
    @PostMapping("/editBook")
    public ModelAndView edit(BookInfo bookInfo) {
        log.info("edit修改后的书籍信息", bookInfo);

        bookInfoService.update(bookInfo);
        List<BookInfo> list = bookInfoService.findAll();
        ModelAndView mav = new ModelAndView("bookinfo-list");
        mav.addObject("bookInfos", list);
        mav.addObject("succ", "更新成功！！！");
        return mav;
    }


}
